package SelfCrossing;

public class Solution {
    public boolean isSelfCrossing(int[] x) {
        if (x.length<=3) return false;
        boolean up=false;
        int lastx=x[0],lasty=x[1];
        
        for (int i=2;i<x.length;i++){
        	if (up==false){
        		if (i%2==0)
        			{
        			if (x[i]<=lastx) {
        				up=true;
        				if (x[i]+(i-4>=0?x[i-4]:0)>=x[i-2]) lasty=x[i-1]-(i-3>=0?x[i-3]:0);
        			    }
        			}
        		else
        		{
        			if (x[i]<=lasty) {
        				up=true;
        				if (x[i]+(i-4>=0?x[i-4]:0)>=x[i-2]) lastx=x[i-1]-(i-3>=0?x[i-3]:0);
        			    }
        		}
        	}
        	else{
        		if (i%2==0)
        			{if (x[i]>=lastx) return true;}
        		else
        			{if (x[i]>=lasty) return true;}
        	}
            if (i%2==0) lastx=x[i];
            else lasty=x[i];
            
        }
        return false;
    }
    public static void main(String args[]){
    	int[] t={2,1,4,4,3,3,2,1,1};
    	Solution so=new Solution();
    	System.out.println(so.isSelfCrossing(t));
    }
}
